package com.example.myapplication.Services;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.R;

public class MusicActivity extends AppCompatActivity {
  TextView textView;
  Intent musicServiceIntent;
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.service_music_layout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("音乐播放");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Init();

  }

  private void Init() {
    musicServiceIntent = new Intent(this, MusicService.class);

    textView = findViewById(R.id.music_tip);
    Button playButton = findViewById(R.id.play_button);
    Button pauseButton = findViewById(R.id.pause_button);
    Button stopButton = findViewById(R.id.stop_button);
    SetTipText("testMusic");

    playButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PlayMusic();
      }
    });
    pauseButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        PauseMusic();
      }
    });
    stopButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        StopMusic();
      }
    });
  }

  private void StopMusic() {
    stopService(musicServiceIntent);

  }

  private void PauseMusic() {
  }

  private void PlayMusic() {
    startService(musicServiceIntent);
    Toast.makeText(this, "正在播放音乐", Toast.LENGTH_SHORT).show();

  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }

  private void GoBack() {
    finish();
  }

  private void SetTipText(String newStr) {
    if (newStr == null) {
      return;
    }
    textView.setText(getString(R.string.current_music, newStr));
  }
}
